package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import com.itheima.pojo.Result;

import java.time.LocalDate;

@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    //RequestBody用来接受前端传过来的josn数据
    @GetMapping("/clazzs")
    public Result queryString(
            String name,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<Clazz> pageResult = clazzService.queryString(name , begin , end , page , pageSize);
        return Result.success(pageResult);
    }
    //根据id删除
    //@PathVarible 提取数值绑定到参数上
    //@RequestParam("ID")前端需要的数据映射到id上
    @DeleteMapping("/clazzs/{id}")
    public Result deleteClazz(@PathVariable("id") Integer id){
        clazzService.deleteClazz(id);
        return Result.success();
    }

    //增添数据
    @PostMapping("/clazzs")
    public Result queryPostClazz(@RequestBody Clazz clazz){
        clazzService.queryPostClazz(clazz);
        return Result.success();
    }
    //改的前提是先查询
    @GetMapping("/clazzs/{id}")
    public Result queryClazz(@PathVariable("id") Integer id){
    Clazz clazz = clazzService.queryClazz(id);
    return Result.success(clazz);
    }
    //改
    @PutMapping("/clazzs")
    public Result updateClazz(@RequestBody Clazz clazz){
//        if (clazz.getId() == null) {
//            return Result.error("更新操作必须包含ID");
//        }
        clazzService.updateClazz(clazz);
        return Result.success();
    }

}




